﻿using System;
using System.Collections;
using System.Reflection;

namespace SimpleORM
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public sealed class ExpressionTranslaterAttribute : Attribute
    {
        #region Properties

        public string MethodName { get; set; }

        #endregion

        #region Ctor

        public ExpressionTranslaterAttribute()
        {
        }

        #endregion

        #region Methods

        public static Hashtable CreateHandlers(Type type)
        {
            Hashtable ht = new Hashtable();

            MethodInfo[] listMethods = type.GetMethods();

            foreach (MethodInfo mi in listMethods)
            {
                ExpressionTranslaterAttribute[] attrs =
                    mi.GetAttributes<ExpressionTranslaterAttribute>();

                foreach (ExpressionTranslaterAttribute attr in attrs)
                {
                    if (string.IsNullOrWhiteSpace(attr.MethodName))
                        attr.MethodName = mi.Name;

                    if (!ht.ContainsKey(attr.MethodName))
                        ht[attr.MethodName] = mi;
                }
            }

            return ht;
        }

        #endregion
    }
}